#!/usr/bin/perl
use strict;

BEGIN {
	use lib qw ( /usr/local/EPNG/cgi-lib/perl );
  $ENV{PGPLOT_BACKGROUND} = 'white';
  $ENV{PGPLOT_FOREGROUND} = 'black';
};

use EP::Visualization::Grid;
use EP::Visualization::LineGraphic;

use PDL;
use PDL::NiceSlice;

use Getopt::Long;
use Pod::Usage;

my $man = 0;
my $help = 0;
my $verbose = 0;
my $plotheight = 250;
my $cellwidth  = 25;
my $svg;
my $png;

GetOptions ( 'help|?'   => \$help,
	     man        => \$man,
	     verbose    => \$verbose,
	     plotheight => \$plotheight,
	     cellwidth  => \$cellwidth,
	     'svg=s'    => \$svg,
	     'png=s'    => \$png
	   ) or pod2usage(2);

pod2usage(1) if $help;
pod2usage(-exitstatus => 0, -verbose => 2) if $man;

pod2usage("Cannot write both PNG and SVG to stdout") if $svg eq "-" and $png eq "-";

open STDERR, ">/dev/null" if not $verbose;

my $matrix_file = $ARGV[0];
pod2usage( "Need to pass data matrix file as first argument" ) if ( not -s $matrix_file );

my $svgout;
if ( $svg and $svg ne "-" ) {
  open $svgout, ">$svg" or die "Couldn't open $svg for output: $!";
  warn "Writing SVG output to $svg" if $verbose;
} else {
  $svgout = \*STDOUT if $svg eq "-";
  warn "Writing SVG output to stdout" if $verbose and $svg eq "-";
}

my $pngout;
if ( $png and $png ne "-" ) {
  open $pngout, ">$png" or die "Couldn't open $png for output: $!";
  warn "Writing PNG output to $png" if $verbose;
} else {
  $pngout = \*STDOUT if $png eq "-";
  warn "Writing PNG output to stdout" if $verbose and $png eq "-";
}

my $grid = new EP::Visualization::Grid (1,1);

my @data = rcols ( $matrix_file, { DEFTYPE => float, PERLCOLS => [0] } );
my $d = transpose ( cat ( @data[1..$#data-1] ) );

warn "Read " . $d->info if $verbose;
my $lineplot = new EP::Visualization::LineGraphic ( data        => $d,
						    plot_height => $plotheight,
						    cell_width  => $cellwidth,
						    colors      => $data[$#data],
						    timeseries  => 0
						  );

$grid->fillCellArray ( [[ $lineplot ]] );

print $svgout $grid->to_svg ( zoom => 0         ) if $svgout;
              $grid->to_png ( pngout => $pngout ) if $pngout;

close $svgout if $svgout;
close $pngout if $pngout;

__END__

=head1 NAME

lineplot - Uses C<EP::Visualization> to produce line plots

=head1 SYNOPSIS

lineplot [options] [tab-delimited data file]

=head1 OPTIONS

=over 8

=item B<-plotheight>

Height of the plot (default: 150). In pixels.

=item B<-cellwidth>

Width of each cell/condition/column (default: 12). In pixels.

=item B<-svg>

Name of output file for SVG output. If set to -, will print to stdout.

=item B<-png>

Name of output file for PNG output. If set to -, will print to stdout.

=item B<-verbose>

Produce verbose processing output.

=item B<-help>

Print a brief help message and exit.

=item B<-man>

Prints the manual page and exits.

=back

=head1 DESCRIPTION

B<lineplot> will read the given input file, a tab-delimited data matrix without any row or column headers and produce an
SVG lineplot, with one line for each row of the matrix.

=cut
